package com.jeasonzhao.commons.basic;

import com.jeasonzhao.commons.utils.RegexHelper;

public final class StringCollection extends java.util.Vector<String>
{
    private static final long serialVersionUID = 1L;
    public StringCollection()
    {

    }
    public StringCollection(Iterable<?> er)
    {
        if(null == er)
        {
            return;
        }
        else
        {
            for(Object obj : er)
            {
                this.add(obj == null ? null : obj.toString());
            }
        }
    }

    public StringCollection(Object ...params)
    {
        if(null != params)
        {
            for(Object s : params)
            {
                this.add(s == null ? null : s.toString());
            }
        }
    }
    public static StringCollection splitString(String str,char... sep)
        {
            if(null == str || str.length() < 1)
            {
                return null;
            }
            else if(null == sep || sep.length < 1)
            {
                return new StringCollection(str);
            }
            else
            {
                String seps="";
                for(char c : sep)
                {
                    if(seps.length()>0)
                    {
                        seps+="|";
                    }
                    if(c==' '||c=='\t'||c=='\n'||c=='\r')
                    {
                        seps+="\\s";
                    }
                    else if(c=='|'||c=='('||c==')'||c=='*'||c=='+'
                        ||c=='^'||c=='$'||c=='\\'||c=='.'||c=='?'
                        ||c=='{'||c=='}'||c=='['||c==']')
                    {
                        seps+="\\"+c;
                    }
                    else
                    {
                        seps+=c;
                    }
                }
                System.out.println(seps);
                if(seps.length()<1)
                {
                    return new StringCollection(str);
                }
                else
                {
                    return new StringCollection((Object[]) str.split(seps));
                }
            }
    }
    public static StringCollection splitString(String str,String sep)
    {
        if(null == str || str.length() < 1)
        {
            return null;
        }
        else if(null == sep || sep.length() < 1)
        {
            return new StringCollection(str);
        }
        else
        {
            return new StringCollection((Object[]) str.split(sep));
        }
    }

    public void toLowerCase()
    {
        for(int n = 0;n < this.size();n++)
        {
            String str = this.get(n);
            str = str == null ? null : str.toLowerCase();
            this.set(n,str);
        }
    }

    public void toUpperCase()
    {
        for(int n = 0;n < this.size();n++)
        {
            String str = this.get(n);
            str = str == null ? null : str.toUpperCase();
            this.set(n,str);
        }
    }

    public StringCollection removeEmptyItems()
    {
        for(int n = 0;n < this.size();n++)
        {
            String str = this.get(n);
            if(null == str || str.trim().length() < 1)
            {
                this.remove(n);
                n--;
            }
        }
        return this;
    }

    public StringCollection trim()
    {
        for(int n = 0;n < this.size();n++)
        {
            String str = this.get(n);
            if(null == str || str.trim().length() < 1)
            {
                this.remove(n);
                n--;
            }
            else
            {
                this.set(n,str.trim());
            }
        }
        return this;
    }

    public void removeHead()
    {
        if(this.size() > 0)
        {
            this.removeElementAt(0);
        }
    }

    public void push(String s)
    {
        this.insertElementAt(s,0);
    }

    public String pop()
    {
        if(this.size() > 0)
        {
            String s = this.elementAt(0);
            this.removeElementAt(0);
            return s;
        }
        else
        {
            return null;
        }
    }

    public String popTail()
    {
        if(this.size() > 0)
        {
            String s = this.elementAt(this.size() - 1);
            this.removeElementAt(this.size() - 1);
            return s;
        }
        else
        {
            return null;
        }
    }
    public StringCollection exclude(String ...s)
    {
        return this.removeItems(s);
    }
    public StringCollection removeItems(String ...s)
    {
        if(null == s)
        {
            return this;
        }
        for(String x : s)
        {
            if(null != x)
            {
                this.remove(x);
            }
        }
        return this;
    }

    public void removeTail()
    {
        if(this.size() > 0)
        {
            removeElementAt(this.size() - 1);
        }
    }

    public String toString()
    {
        return this.toString(",",null,false);
    }

    public String toString(String str,boolean appendAtFirst)
    {
        return toString(str,null,appendAtFirst);
    }

    public String toString(String str,String strPrefix,boolean appendAtFirst)
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;n < this.size();n++)
        {
            if(n > 0 || appendAtFirst)
            {
                strRet.append(str);
            }
            strRet.append((strPrefix == null ? "" : strPrefix) + this.elementAt(n));
        }
        return strRet.toString();
    }

    public static String toString(Object[] ar,String str,boolean appendAtFirst)
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;null != ar && n < ar.length;n++)
        {
            if((n > 0 || appendAtFirst) && null != str)
            {
                strRet.append(str);
            }
            strRet.append(ar[n]);
        }
        return strRet.toString();

    }

    public static String toString(int[] ar,String str,boolean appendAtFirst)
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;null != ar && n < ar.length;n++)
        {
            if(n > 0 || appendAtFirst)
            {
                strRet.append(str);
            }
            strRet.append(ar[n]);
        }
        return strRet.toString();
    }

    public static String toString(double[] ar,String str,boolean appendAtFirst)
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;null != ar && n < ar.length;n++)
        {
            if(n > 0 || appendAtFirst)
            {
                strRet.append(str);
            }
            strRet.append(ar[n]);
        }
        return strRet.toString();
    }

    public String reverseWildcardMatches(String strRegex)
    {
        for(int n = this.size() - 1;n >= 0;n--)
        {
            if(RegexHelper.wildcard(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public String wildcardMatches(String strRegex)
    {
        for(int n = 0;n < this.size();n++)
        {
            if(RegexHelper.wildcard(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public String matches(String strRegex)
    {
        for(int n = 0;n < this.size();n++)
        {
            if(RegexHelper.matches(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public String matchesIgnoreCase(String strRegex)
    {
        for(int n = 0;n < this.size();n++)
        {
            if(RegexHelper.matchesIgnoreCase(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public String wildcardMatchesIgnoreCase(String strRegex)
    {
        for(int n = 0;n < this.size();n++)
        {
            if(RegexHelper.wildcardIgnoreCase(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public String reverseWildcardMatchesIgnoreCase(String strRegex)
    {
        for(int n = this.size() - 1;n >= 0;n--)
        {
            if(RegexHelper.wildcardIgnoreCase(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public String reverseMatches(String strRegex)
    {
        for(int n = this.size() - 1;n >= 0;n--)
        {
            if(RegexHelper.matches(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public String reverseMatchesIgnoreCase(String strRegex)
    {
        for(int n = this.size() - 1;n >= 0;n--)
        {
            if(RegexHelper.matchesIgnoreCase(strRegex,this.elementAt(n)))
            {
                return this.elementAt(n);
            }
        }
        return null;
    }

    public StringCollection find(String strRegex)
    {
        StringCollection ret = new StringCollection();
        if(null == strRegex)
        {
            return ret;
        }
        for(String strValue : this)
        {
            if(RegexHelper.matches(strRegex,strValue))
            {
                ret.addElement(strValue);
            }
        }
        return ret;
    }

    public StringCollection findIgnoreCase(String strRegex)
    {
        StringCollection ret = new StringCollection();
        if(null == strRegex)
        {
            return ret;
        }
        for(String strValue : this)
        {
            if(RegexHelper.matchesIgnoreCase(strRegex,strValue))
            {
                ret.addElement(strValue);
            }
        }
        return ret;
    }

    public boolean isContains(String strRegEx)
    {
        StringCollection coll = this.find(strRegEx);
        return null != coll && coll.size() > 0;
    }

    public boolean isContainsIgnoreCase(String strRegEx)
    {
        StringCollection coll = this.findIgnoreCase(strRegEx);
        return null != coll && coll.size() > 0;
    }

    public void uniqueItems()
    {
        java.util.HashSet<String> set = new java.util.HashSet<String>();
        for(int n = 0;n < this.size();n++)
        {
            if((null == this.get(n) || this.get(n).trim().length() < 1) ||
               set.contains(this.get(n)))
            {
                this.removeElementAt(n);
                n--;
            }
            else
            {
                set.add(this.get(n));
            }
        }

    }

    public StringCollection add2(String ...arys)
    {
        if(null != arys)
        {
            for(String s : arys)
            {
                this.add(s);
            }
        }
        return this;
    }
}
